package com.relife.core.easyui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.relife.core.service.BaseService;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;



@Component
public class ActivityHistoryTag extends TagSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static BaseService service = null;
	private String pid;

	@Override
	public int doStartTag() throws JspTagException {
		return EVAL_PAGE;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			if (service == null) {
				WebApplicationContext webApp = WebApplicationContextUtils.getWebApplicationContext(this.pageContext.getServletContext());
				service = (BaseService) webApp.getBean("service");
			}
			JspWriter out = this.pageContext.getOut();
			StringBuffer sb = new StringBuffer();
			sb.append(this.getHtml());
			out.print(sb);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	private StringBuffer getHtml() {
		Map<String, Map<String, Object>> map = new HashMap<String, Map<String, Object>>();
		List<Map<String, Object>> maplist = service.getActivityHistory(pid);
		for (int i = 0; i < maplist.size(); i++) {
			map.put((String) maplist.get(i).get("TASKKEY"), maplist.get(i));
		}

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> v : map.values()) {
			list.add(v);
		}

		// list排序
		Collections.sort(list, new ComparatorUser());

		StringBuffer sb = new StringBuffer("");
		int size = list.size();
		sb.append("<table>\n");
		sb.append("<tr>\n");
		for (int i = 0; i < list.size(); i++) {
			sb.append("<td style=\"padding:2px 0px;border-top: 0px;width:" + 800 / size + "px;" + (i == 0 ? "border-left: 0px" : "") + "\">");
			sb.append("<span style=\"text-align: left;\">" + list.get(i).get("NAME"));
			sb.append("：" + isEmpty(list.get(i).get("MESSAGE"), "同意") + "</span>\n");
			sb.append("<span style=\"text-align: right;\">" + list.get(i).get("USERNAME") + " " + list.get(i).get("SHOWTIME") + "</span>\n");
			sb.append("</td>\n");
		}
		sb.append("</tr>\n");
		sb.append("</table>\n");
		return sb;
	}

	private String isEmpty(Object obj, String ret) {
		if (obj != null && !("").equals(obj)) {
			return obj.toString();
		}
		return ret;
	}

	private class ComparatorUser implements Comparator<Map<String, Object>> {

		public int compare(Map<String, Object> m, Map<String, Object> m1) {
			Date date1 = (Date) m.get("ENDTIME");
			Date date2 = (Date) m1.get("ENDTIME");
			if (date1.after(date2)) {
				return 1;
			} else {
				return -1;
			}
		}
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

}
